$().ready(function() {
	validateRule();
	$("#topicTypes").val($("#topicTypesHidden").val());
	$("#type").val($("#typeHidden").val());
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});

//证书下拉框获取知识点数据
byCourToChapList($("#coursesIdHidden").val());
$("#coursesId").change(function () {
	byCourToChapList($(this).val())
});
function byCourToChapList(coursesId) {
	$("#chapterId").empty();
	$.ajax({
		url: '/jzApi/utils/byCourToChapList',
		data :{coursesId:coursesId} ,
		success: function (r) {
			var val =$("#chapterIdHidden").val();
			if(r.data.length==0){
				$("#chapterId").append('<option value="">暂无数据</option>');
			}else{
				$("#chapterId").append('<option value="">请选择</option>');
				for(var p in r.data){
					if(val!=r.data[p].id){
						$("#chapterId").append('<option value="'+r.data[p].id+'">'+r.data[p].name+'</option>');
					}else{
						$("#chapterId").append('<option selected  value="'+r.data[p].id+'">'+r.data[p].name+'</option>');
					}
				}
			}
		}
	})
}


function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/chapterItemBank/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			coursesId : {
				required : true
			},
			chapterId : {
				required : true
			},
			type : {
				required : true
			},
			topicTypes : {
				required : true
			},
			contents : {
				required : true
			},
			options : {
				required : true
			},
			rightKey : {
				required : true
			},
			topicAnalysis : {
				required : true
			},
			score : {
				required : true
			}
		},
		messages : {
			coursesId : {
				required : icon + "请选择课程"
			},
			chapterId : {
				required : icon + "请选择章节"
			},
			type : {
				required : icon + "请选择类型"
			},
			topicTypes : {
				required : icon + "请选择题目类型"
			},
			contents : {
				required : icon + "请输入题目内容"
			},
			options : {
				required : icon + "请输入答案选项"
			},
			rightKey : {
				required : icon + "请输入正确答案"
			},
			topicAnalysis : {
				required : icon + "请输入题目解析"
			},
			score : {
				required : icon + "请输入分值"
			}
		}
	})
}